<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Accomodation extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "accomodation";
		$this->prefix = "acc";
	}

	function getAccomodation($where = NULL, $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "accName", $dir = "ASC") {

		$fieldsQuery = Common::fieldsquery($fields, $query);
		$order = Common::coalesce(array($order, "accName"));
		$dir = Common::coalesce(array($dir, "ASC"));

		$qs =	"
					select
						acc_id, acc_name, acc_url_name, acc_city_id, acc_latitude, acc_longitude, acc_presenter, ins_name, city_name, city_url_name, t4.e2c_temp_flag,
						
						group_concat(distinct(ifnull(ins_id, '')) separator ',') as insList,
						if(left(group_concat(distinct(ifnull(ins_name, '')) order by ins_name separator ', '),1) = ',', substr(group_concat(distinct(ifnull(ins_name, '')) order by ins_name separator ', '),2), group_concat(distinct(ifnull(ins_name, '')) order by ins_name separator ', ')) as insNameList
						
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."city on city_id = acc_city_id and city_del = 0

						".((in_array($this->uri->segment(1), array("admin", "admin_lte")) and Common::isAdmin())?(""):("left"))." join ".$this->db->dbprefix."acc2ins_maj on a2im_acc_id = acc_id and a2im_del = 0
						".((in_array($this->uri->segment(1), array("admin", "admin_lte")) and Common::isAdmin())?(""):("left"))." join ".$this->db->dbprefix."institute on ins_id = a2im_ins_id and ins_del = 0
						
						".((in_array($this->uri->segment(1), array("admin", "admin_lte")) and Common::isAdmin())?
						(" join ".$this->db->dbprefix."usr2ent on u2e_del = 0 and u2e_usr_id = '".$this->session->userdata("usrId")."' and (a2im_ins_id = u2e_ins_id) and u2e_page_cat = 'ACC' "):(""))."
						
						left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'ACC' and acc_id = e2c_ent_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null
						left join (
							select
								e2c_ent_id, e2c_temp_flag
							from
								".$this->db->dbprefix."ent2cat 
							where
								e2c_entity = 'ACC' and e2c_del = 0 and e2c_temp_flag = 'I'
							group by
								e2c_ent_id
						) as t4 on t4.e2c_ent_id = acc_id
					where
						acc_del = 0 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					group by
						acc_id
					order by
						".(($order == "rand()")?($order):(Common::underscore($order)." ".$dir))."
					".((is_numeric($start))?(" limit ".$start.",".$limit):(""))."
						";
		$result = $this->db->query($qs);
		//Common::pr($this->db->last_query());

		$getAccomodation = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$getAccomodation[] = array(
					"accId" => $row->acc_id,
					"accName" => $row->acc_name,
					"accUrlName" => $row->acc_url_name,
					"insList" => $row->insList,
					"insNameList" => $row->insNameList,
					"accCityId" => $row->acc_city_id,
					"cityName" => $row->city_name,
					"accLatitude" => $row->acc_latitude,
					"accLongitude" => $row->acc_longitude,
					"e2cTempFlag" => $row->e2c_temp_flag,
					"accPresenter" => $row->acc_presenter,
				);
			}
		}

		return $getAccomodation;
	}

	function getAccomodationCount($where = NULL, $fields, $query) {

		$fieldsQuery = Common::fieldsquery($fields, $query);

		$qs =	"
					select
						count(*) as cnt
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."city on city_id = acc_city_id and city_del = 0

						".((in_array($this->uri->segment(1), array("admin", "admin_lte")) and Common::isAdmin())?(""):("left"))." join ".$this->db->dbprefix."acc2ins_maj on a2im_acc_id = acc_id and a2im_del = 0
						".((in_array($this->uri->segment(1), array("admin", "admin_lte")) and Common::isAdmin())?(""):("left"))." join ".$this->db->dbprefix."institute on ins_id = a2im_ins_id and ins_del = 0
						
						".((in_array($this->uri->segment(1), array("admin", "admin_lte")) and Common::isAdmin())?
						(" join ".$this->db->dbprefix."usr2ent on u2e_del = 0 and u2e_usr_id = '".$this->session->userdata("usrId")."' and (a2im_ins_id = u2e_ins_id) and u2e_page_cat = 'ACC'"):(""))."

					where
						acc_del = 0 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					";
		$result = $this->db->query($qs);

		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				return $row->cnt;
			}
		}
	}

}